package com.eorchis.module.addUser.ui.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eorchis.constants.DeptConstants;
import com.eorchis.constants.RoleConstants;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.addUser.bean.DepartmentBean;
import com.eorchis.module.addUser.bean.UserBean;
import com.eorchis.module.addUser.service.IAddUserService;
import com.eorchis.module.addUser.ui.commond.UserQueryCommond;
import com.eorchis.module.addUser.ui.commond.UserValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

@Controller("addUserController")
@RequestMapping(value = AddUserController.MODULE_PATH)
public class AddUserController extends AbstractBaseController<UserValidCommond,UserQueryCommond>{

	// 模块页面根路径
	public static final String MODULE_PATH = "/module/addUser";
	// 班级审核人员信息接口
	@Autowired
	@Qualifier("com.eorchis.module.addUser.service.impl.IAddUserServiceImpl")
	private IAddUserService addUserService;

	@Override
	public IBaseService getService() {
		return addUserService;
	}
	
	/**
	 * 按数量新增部委
	 * @Date 2016-09-14
	 * @author RongSL
	 * */
	@RequestMapping("/addBwAdmin")
	public String addBwAdmin(@ModelAttribute("resultList")UserQueryCommond commond,@ModelAttribute("resultState")ResultState resultState,HttpServletRequest request,HttpServletResponse response){
		try{
			if(PropertyUtil.objectNotEmpty(commond.getUserNumber())&&PropertyUtil.objectNotEmpty(commond.getDepId())){
				List<DepartmentBean> list=addUserService.getDepartmentsList(commond);
				if(PropertyUtil.objectNotEmpty(list)){
					boolean flag=addUserService.addBwAdmin(commond);
					if(flag){
						resultState.setMessage("操作成功！");
						resultState.setState(ResultState.RESULT_STATE_SUCCESS);
					}else{
						resultState.setMessage("操作失败！");
						resultState.setState(ResultState.RESULT_STATE_FAIL);
					}
				}else{
					resultState.setMessage("此部门不存在！");
					resultState.setState(ResultState.RESULT_STATE_FAIL);
				}
			}else{
				resultState.setMessage("缺少参数！");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 启用/作废部委
	 * @Date 2016-09-14
	 * @author RongSL
	 * */
	@RequestMapping("/EnableBwAdmin")
	public String EnableBwAdmin(@ModelAttribute("resultList")UserQueryCommond commond,@ModelAttribute("resultState")ResultState resultState,HttpServletRequest request,HttpServletResponse response){
		try{
			boolean flag=addUserService.EnableBwAdmin(commond);
			if(flag){
				resultState.setMessage("操作成功！");
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			}else{
				resultState.setMessage("操作失败！");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 按条件查询部委
	 * @Date 2016-09-14
	 * @author RongSL
	 * */
	@RequestMapping("/findBwAdmin")
	public String findBwAdmin(@ModelAttribute("resultList")UserQueryCommond commond,@ModelAttribute("resultState")ResultState resultState,HttpServletRequest request,HttpServletResponse response){
		try{
			commond.setRoleCode(RoleConstants.ROLE_CODE_BW_ADMIN);
			if(PropertyUtil.objectNotEmpty(commond.getDepId())){
				List<UserBean> list=addUserService.findBwAdmin(commond);
				commond.setResultList(list);
				resultState.setMessage("查询成功！");
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			}else{
				resultState.setMessage("缺少参数！");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}catch(Exception e){
			e.printStackTrace();
			resultState.setMessage("查询失败！");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	/**
	 * 重置指定部委密码
	 * @Date 2016-09-14
	 * @author RongSL
	 * */
	@RequestMapping("/resetBwAdmin")
	public String resetBwAdmin(@ModelAttribute("resultList")UserQueryCommond commond,@ModelAttribute("resultState")ResultState resultState,HttpServletRequest request,HttpServletResponse response){
		try{
			boolean flag=addUserService.resetBwAdmin(commond);
			if(flag){
				resultState.setMessage("操作成功！");
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			}else{
				resultState.setMessage("操作失败！");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 获得部委树
	 * @Date 2016-09-14
	 * @author RongSL
	 * */
	@RequestMapping("/getDepartmentsList")
	public String getDepartmentsList(@ModelAttribute("resultList")UserQueryCommond commond,@ModelAttribute("resultState")ResultState resultState,HttpServletRequest request,HttpServletResponse response){
		try{
			commond.setDepParentId(DeptConstants.BW_DEPT_ROOT_ID);
			List<DepartmentBean> list=addUserService.getDepartmentsList(commond);
			commond.setResultList(list);
			resultState.setMessage("操作成功！");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
}
